<?php

/*
 *  老订单 使用当前文件跑一遍生成预估距离
 */

$serverFilePath =realpath(dirname(__FILE__)); ;//realpath(dirname(__FILE__)); //$_SERVER["SCRIPT_FILENAME"];
$doc_dir = strrpos($serverFilePath, ''.DIRECTORY_SEPARATOR.'crontab');
$doc_dir = substr($serverFilePath, 0, $doc_dir + 1);   //project root
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'lib/Config.php';
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.'HybrisClass.php';
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.'Logging.php';
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.'GaoDeAPIClass.php';

set_time_limit(0);
error_reporting(E_ALL);
header("Content-type:text/html;charset:utf-8");

$log = new Logging();
$log->lfile('order_sys');
$config_ini = $doc_dir . 'config/config.ini';

$ini_array = parse_ini_file($config_ini, true);
$db_array = $ini_array['DB'];
$getWebsite = $ini_array['WebSite'];
$con = mysqli_init();

mysqli_real_connect($con, $db_array['host'], $db_array['id'], $db_array['pwd'], $db_array['database'], $db_array['port']);
mysqli_set_charset($con, $db_array['encoding']);

$sql = " SELECT id,ph_no,order_sys FROM orders_express where  status = 5 AND order_sys < 4";
$result = mysqli_query($con, $sql);
$res = array();
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $res[] = $row;
}
echo'<pre>';
var_dump($res);die;
$OrderSYSUrl = '接单系统地址';
if($res && count($res)){
    foreach ($res as $k => $val) {
        $log->lwrite ('发送数据：'.json_encode($val, JSON_UNESCAPED_UNICODE));

        $data['ph_no'] = $val['ph_no'];
        $res_sys = HttpPost($OrderSYSUrl,json_encode($data),'json');
        $log->lwrite ('接收返回：'.$res_sys);
        if(strtolower($res_sys) === 'success')
        {
            $columns = "order_sys = 9";
            $msg = '推送成功';
        } else {
            if($val['order_sys']  == 1){
                $columns = "order_sys = 2";
            }else{
                $columns = "order_sys = ".($val['order_sys'] + 1);
            }
            $msg = '推送失败';
        }
        $sql_edit = " update orders set $columns where id = ".$val['id'];
        $result = mysqli_query($con, $sql_edit);
        $log->lwrite ('修改数据：'.$sql_edit);
        echo 'id:'.$val['id'].$msg;
    }

}else{
    echo  '无需推送'.'<br/>';
}

function HttpPost($url, $param, $ContentType = ''){
    $log = new Logging();
    $oCurl = curl_init();
    if (stripos($url, "https://") !== FALSE) {
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
    }
    if (empty($ContentType)) {
        curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('application/x-www-form-urlencoded; charset=utf-8'));
    } else if ($ContentType == "json") {
        curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
    }

    $strPOST='';
    if (is_string($param)) {
        $strPOST = $param;
    } else {
        $aGET = array();
        foreach ($param as $key => $val) {
            $aGET[] = $key . "=" . ($val);
        }
        $strPOST = join("&", $aGET);
    }

    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($oCurl, CURLOPT_TIMEOUT, 60);
    curl_setopt($oCurl, CURLOPT_POST, true);
    curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
    curl_setopt($oCurl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

    $time=strtotime(date('Y-m-d H:i:s'));
    $log->lfile('mt_Heavy_push');
    $log->lwrite($time.','.($url));
    $log->lwrite($time.','.$strPOST);
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    $log->lwrite($time.' receive:'.($sContent));
    curl_close($oCurl);
    return $sContent;
}

